package com.example.demo.controller;

import com.example.demo.entity.Userinfo;
import com.example.demo.service.LogService;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * Created with IntelliJ IEDA.
 * Description:
 * User:86186
 * Date:2023-08-18
 * Time:14:22
 */

@RestController
@RequestMapping("/user")
public class UserController<result> {

    @Autowired
    private UserService userService;

    //事务管理器,通过事务管理器可以拿到事务
    @Autowired
    private DataSourceTransactionManager dataSourceTransactionManager;

    // 定义事务属性
    @Autowired
    private TransactionDefinition transactionDefinition;

    @Autowired
    private LogService logService;

    @RequestMapping("/add")
    public int add(Userinfo userinfo){
        // 非空校验
        if (userinfo == null || !StringUtils.hasLength(userinfo.getUsername())
                || !StringUtils.hasLength(userinfo.getPassword())) {
            return 0;
        }

        // 1.开启事务.就是得到并开启事务
        TransactionStatus transactionStatus =
                dataSourceTransactionManager.getTransaction(transactionDefinition);

        // 手动设置创建时间和修改时间的值
        userinfo.setCreatetime(LocalDateTime.now().toString());
        userinfo.setUpdatetime(LocalDateTime.now().toString());
        int result = userService.add(userinfo);
        System.out.println("添加:" + result);

        // 2.提交事务
        dataSourceTransactionManager.commit(transactionStatus);

//        // 2.回滚事务
//        dataSourceTransactionManager.rollback(transactionStatus);


        return result;
    }

    @Transactional(propagation = Propagation.NESTED)// 声明式事务（自动提交）
    @RequestMapping("/add2")
    public Integer add2(Userinfo userinfo){
        // 非空校验
        if (userinfo == null || !StringUtils.hasLength(userinfo.getUsername())
                || !StringUtils.hasLength(userinfo.getPassword())) {
            return 0;
        }
        int result = userService.add(userinfo);
        if (result > 0){
            logService.add();
        }
        //        System.out.println("add2:" + result);
//        try {
//            int num = 10/0;
//        } catch (Exception e) {
//            System.out.println(e.getMessage() );
            // 1.将异常继续抛出
//            throw e;
//            System.out.println(e.getMessage());
            // 2.手动回滚事务
//            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//        }
        return result;
    }

}
